import Vue from 'vue'
import VueI18n from 'vue-i18n'

/* import enLocale from 'element-ui/lib/locale/lang/en'
import zhLocale from 'element-ui/lib/locale/lang/zh-CN'
import twLocale from 'element-ui/lib/locale/lang/zh-TW'
import kolocale from 'element-ui/lib/locale/lang/ko'
import jaLocale from 'element-ui/lib/locale/lang/ja'
import ptlocale from 'element-ui/lib/locale/lang/pt'
import esLocale from 'element-ui/lib/locale/lang/es'
import delocale from 'element-ui/lib/locale/lang/de'
import frLocale from 'element-ui/lib/locale/lang/fr'
import swlocale from 'element-ui/lib/locale/lang/sw'
import vilocale from 'element-ui/lib/locale/lang/vi' */

Vue.use(VueI18n)

export default ({ app, store }) => {
  if (!Vue.__UseVueI18n__) {
    Vue.use(VueI18n)
    Vue.__UseVueI18n__ = true
  }

  // Set i18n instance on app
  // This way we can use it in middleware and pages asyncData/fetch
  let options = store.state.languagesOptions
  let messages = {}
  /* let eleLocales = {
    en: enLocale,
    zh: zhLocale,
    tw: twLocale,
    ko: kolocale,
    ja: jaLocale,
    pt: ptlocale,
    es: esLocale,
    de: delocale,
    fr: frLocale,
    sw: swlocale,
    vi: vilocale,
  } */
  options.forEach((item) => {
    messages[item.value] = {
      /* ...eleLocales[item.value].el, */
      ...require(`@/locales/${item.value}.json`),
    }
  })

  app.i18n = new VueI18n({
    silentTranslationWarn: false,
    locale: store.state.locale,
    fallbackLocale: 'en', //我这里默认语言为英文
    messages: messages,
  })

  app.i18n.path = (link) => {
    console.log(link)
    // 如果是默认语言，就省略
    if (app.i18n.locale === app.i18n.fallbackLocale) {
      return `/${link}`
    }
    return `/${app.i18n.locale}/${link}`
  }
}
